package com.shitou.springai1.admin.compoment;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
/**
 * @ClassName CommonMetaHandle
 * @Description 公共元数据处理类
 * @Author shitou
 * @Date 2022/6/14 10:44
 * @Version 1.0
 */

@Component
public class CommonMetaHandle implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        // 填充创建时间（适用于UserDto等包含registerTime字段的实体）
        this.strictInsertFill(metaObject, "registerTime", LocalDateTime.class, LocalDateTime.now());
        // 填充更新时间
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        // 保留对startTime字段的支持（如果其他实体使用）
        if (metaObject.hasGetter("startTime")) {
            this.strictInsertFill(metaObject, "startTime", LocalDateTime.class, LocalDateTime.now());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 更新时只填充更新时间
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
}
